<?php

namespace app\admin\model;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $table = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'payment_time_text',
        'review_status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }

    public function getReviewStatusList()
    {
        return ['0' => __('Review_status 0'), '1' => __('Review_status 1')];
    }

    public function getPaymentMethodList()
    {
        return ['1' => __('Payment_method 1'), '2' => __('Payment_method 2')];
    }

    public function getTouristsTypeList()
    {
        return ['id_card' => __('Tourists type id_card'), 'passport' => __('Tourists type passport')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getPaymentTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['payment_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReviewStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['review_status'] ?? '');
        $list = $this->getReviewStatusList();
        return $list[$value] ?? '';
    }

    protected function setPaymentTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function coupon()
    {
        return $this->belongsTo('Coupon', 'coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function distributor()
    {
        return $this->belongsTo('User', 'distributor_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function verifier()
    {
        return $this->belongsTo('User', 'verifier_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
